package com.kclm.se.util;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

/************************************
 * @params:

 * @Author: sibyl·fung

 * @Date:

 */
public class DateUtil {

    public static Date toDate(LocalDate ldt) {
        if(ldt != null) {
            ZonedDateTime zonedDateTime = ldt.atStartOfDay(ZoneId.systemDefault());
            Instant from = Instant.from(zonedDateTime);
            return Date.from(from);
        }
        return null;
    }

    public static Date toDate(LocalDateTime ldt) {
            if(ldt != null) {
                ZonedDateTime zonedDateTime = ldt.atZone(ZoneId.systemDefault());
                Instant from = Instant.from(zonedDateTime);
                return Date.from(from);
            }
            return null;
    }

    public static LocalDate toLocalDate(Date date) {
        if (date != null) {
            if(date instanceof java.sql.Date) {
                date = new Date(date.getTime());
            }
            Instant instant = date.toInstant();
            //
            ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());
            //
            return zdt.toLocalDate();
        }
        return null;
    }

    public static void main(String[] args) {
        java.sql.Date d = new java.sql.Date(System.currentTimeMillis());
        LocalDate ld = toLocalDate(d);
        System.out.println(ld);
        //
        Date now = toDate(LocalDateTime.now());
        System.out.println(now);
        //
        now = toDate(LocalDate.now());
        System.out.println(now);
    }
}
